package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcEprEntity;
import com.dhcc.cdc.busi.service.ICdcEprService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 电子病历首页、附页等信息
 * 
 * @ClassName: EprController
 * @Description: TODO
 * @author jumper
 * @date 2021-06-09 10:31:50
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/epr")
public class CdcEprController extends BaseController {

	private String PREFIX = "/backend/cdc/epr/";

	@Autowired
	private ICdcEprService eprService;
	

    /**
     * 跳转到电子病历首页、附页等信息列表首页
     */
    @RequestMapping("")
    @Permission("epr:list")
    public String index() {
        return PREFIX + "epr.html";
    }
	
    /**
     * 跳转到新增电子病历首页、附页等信息
     */
    @RequestMapping("/to_add")
    @Permission("epr:add")
    public String toAdd() {
        return PREFIX + "epr_add.html";
    }
	
	/**
     * 跳转到修改电子病历首页、附页等信息
     */
    @RequestMapping("/to_update/{eprId}")
    @Permission("epr:update")
    public String toUpdate(@PathVariable Long eprId, Model model) {
        CdcEprEntity epr = this.eprService.selectById(eprId);
        model.addAttribute("epr",epr);
        return PREFIX + "epr_edit.html";
    }

    /**
     * 获取电子病历首页、附页等信息列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("epr:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcEprEntity> page = new PageFactory<CdcEprEntity>().defaultPage();
        List<CdcEprEntity> result = this.eprService.page(page,params);
        page.setRecords((List<CdcEprEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增电子病历首页、附页等信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("epr:add")
    @BussinessLog(value = "新增电子病历首页、附页等信息")
    public Object add(CdcEprEntity epr) {
        if (CommonUtil.isOneEmpty(epr)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        epr.insert();
        return R.ok();
    }

    /**
     * 删除电子病历首页、附页等信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("epr:delete")
    @BussinessLog(value = "删除电子病历首页、附页等信息")
    public Object delete(@RequestParam Long eprId) {
	
        this.eprService.deleteById(eprId);

        return R.ok();
    }

    /**
     * 修改电子病历首页、附页等信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("epr:update")
    @BussinessLog(value = "修改电子病历首页、附页等信息")
    public Object update(CdcEprEntity epr) {
        if (CommonUtil.isOneEmpty(epr)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcEprEntity old = this.eprService.selectById(epr.getId());

        old.setEpisodeId(epr.getEpisodeId());
        old.setCode(epr.getCode());
        old.setDescription(epr.getDescription());
        old.setValue(epr.getValue());
        old.setDataId(epr.getDataId());
        old.setIsActive(epr.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
